1 module hip.gui.linear_layout;
2 public import hip.gui.widget;
3 import hip.gui.group;
4 
5 
6 class LinearLayout : Group
7 {
8     enum Direction
9     {
10         horizontal,
11         vertical
12     }
13 
14     protected Direction dir;
15     protected int spacing = 0;
16     protected int padding = 0;
17 
18     public void setDirection(Direction d)
19     {
20         dir = d;
21         updateLayout();
22     }
23     void setSpacing(int spacing)
24     {
25         this.spacing = spacing;
26         updateLayout();
27     }
28     void setPadding(int padding)
29     {
30         this.padding = padding;
31         updateLayout();
32     }
33 
34     alias addChild = Widget.addChild;
35     override void addChild(Widget w)
36     {
37         super.addChild(w);
38         updateLayout();
39     }
40 
41 
42     void updateLayout()
43     {
44         int x, y;
45         int maxWidth, maxHeight;
46         foreach(ch; children)
47         {
48             import hip.api;
49             if(!ch.visible) continue;
50             if(dir == Direction.horizontal)
51             {
52                 ch.localTransform.x = x;
53                 if(ch.height > maxHeight)
54                     maxHeight = ch.height;
55                 x+= spacing + ch.width;
56                 maxWidth = x;
57             }
58             else
59             {
60                 ch.localTransform.y = y;
61                 if(ch.width > maxWidth)
62                     maxWidth = ch.width;
63                 y+= spacing + ch.height;
64                 maxHeight = y;
65             }
66         }
67         width = maxWidth;
68         height = maxHeight;
69         setChildrenDirty();
70     }
71     override void preRender()
72     {
73         super.preRender();
74         updateLayout();
75     }
76 }